package com.reggie.dish.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.reggie.uuid.SnowflakeIdUtils;
import com.reggie.flavor.domain.DishFlavor;
import com.reggie.flavor.service.IDishFlavorService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.CategoryService;
import com.ruoyi.system.api.domain.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.reggie.dish.mapper.DishMapper;
import com.reggie.dish.domain.Dish;
import com.reggie.dish.service.IDishService;

/**
 * 菜品管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-27
 */
@Service
public class DishServiceImpl implements IDishService 
{
    @Autowired
    private DishMapper dishMapper;
    @Autowired(required = true)
    private IDishFlavorService flavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询菜品管理
     * 
     * @param id 菜品管理主键
     * @return 菜品管理
     */
    @Override
    public Dish selectDishById(Long id)
    {
        Dish dish = dishMapper.selectDishById(id);
        //查询分类名称
//        ObjectMapper mapper = new ObjectMapper();
//        AjaxResult info = categoryService.getInfo(dish.getCategoryId());
//        Category data = mapper.convertValue(info.get("data"), new TypeReference<Category>() {});
        Category info = categoryService.getInfo(dish.getCategoryId());
        dish.setCategoryName(info.getName());
        dish.setDishFlavors(flavorService.selectDishFlavorByDishId(dish.getId()));
        return dish;
    }

    /**
     * 查询菜品管理列表
     * 
     * @param dish 菜品管理
     * @return 菜品管理
     */
    @Override
    public List<Dish> selectDishList(Dish dish)
    {
        List<Dish> dishes = dishMapper.selectDishList(dish).stream().map(item -> {
//            ObjectMapper mapper = new ObjectMapper();
//            AjaxResult info = categoryService.getInfo(item.getCategoryId());
//            Category data = mapper.convertValue(info.get("data"), new TypeReference<Category>() {});
            Category info = categoryService.getInfo(item.getCategoryId());
            item.setCategoryName(info.getName());
            return item;
        }).collect(Collectors.toList());
        return dishes;
    }

    /**
     * 新增菜品管理
     * 
     * @param dish 菜品管理
     * @return 结果
     */
    @Override
    public int insertDish(Dish dish)
    {
        int i = 0;
        Long dishId = new SnowflakeIdUtils(5, 8).nextId();
        dish.setId(dishId);
        dish.setCreateTime(DateUtils.getNowDate());
        dish.setCreateUser(SecurityUtils.getUserId());
        i += dishMapper.insertDish(dish);

        for (DishFlavor flavor: dish.getDishFlavors()) {
            flavor.setDishId(dishId);
            i+= flavorService.insertDishFlavor(flavor);
        }
        return i;
    }

    /**
     * 修改菜品管理
     * 
     * @param dish 菜品管理
     * @return 结果
     */
    @Override
    public int updateDish(Dish dish)
    {
        int i = 0;
        dish.setUpdateTime(DateUtils.getNowDate());
        dish.setUpdateUser(SecurityUtils.getUserId());
        System.out.println("-------------------------");
        System.out.println("dish:");
        System.out.println(dish);
        System.out.println("dish：：：dishflavor");
        System.out.println(dish.getDishFlavors());
        i += dishMapper.updateDish(dish);

        i += flavorService.updateDishFlavors(dish);
        return i;
    }

    /**
     * 批量删除菜品管理
     * 
     * @param ids 需要删除的菜品管理主键
     * @return 结果
     */
    @Override
    public int deleteDishByIds(Long[] ids)
    {
        return dishMapper.deleteDishByIds(ids);
    }

    /**
     * 删除菜品管理信息
     * 
     * @param id 菜品管理主键
     * @return 结果
     */
    @Override
    public int deleteDishById(Long id)
    {
        return dishMapper.deleteDishById(id);
    }

    @Override
    public int updateDishStatus(Long[] ids, Long status) {
        return dishMapper.updateDishStatus(ids, status);
    }
}
